using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using EntLibEC.Cmn;
using EntLibEC.Biz;
using EntLibEC.Biz.Sale;

namespace EntLibEC.View.Sale
{
	/// <summary>
	/// Summary description for SaleRuleWeb.
	/// </summary>
	public partial class SaleRuleWeb : PageBase
	{
		protected EntLibEC.View.Cmn.BiStatus ucStatus;
		protected EntLibEC.View.Cmn.PM ucPM;
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			myGrid.PageSize = AppConst.PageSize;
			myGrid.PagerStyle.PageButtonCount = AppConst.PageButtonCount;
			btnAdd.Attributes.Add("onClick","javascript:openWindowS2('SaleRuleDetail.aspx');return false;");
			if(!IsPostBack)
			{
				ucStatus.BindStatus(true);
				ucPM.BindPM(true);
			}			
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.myGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.myGrid_PageIndexChanged);

		}
		#endregion

		private void BindGrid()
		{
			string ProductID = txtProductID.Text.Trim();			
			int Status = ucStatus.StatusSysNo;
			Hashtable ht = new Hashtable();
			if(ProductID!="")
				ht.Add("ProductID",ProductID);
			if(Status!=AppConst.IntNull)
				ht.Add("Status",Status);
			if(ucPM.PMSysNo != AppConst.IntNull)
				ht.Add("PMSysNo" , ucPM.PMSysNo);
			DataSet ds = SaleRuleManager.GetInstance().GetSRList(ht);
			ConvertDs(ds);
			base.SetDataGridPage(ds, myGrid);
			myGrid.DataSource = ds.Tables[0];
			myGrid.DataBind();
			Assert(lblmsg,ds.Tables[0].Rows.Count.ToString()+" Records found",1);
		}

		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			BindGrid();
		}		

		private void ConvertDs(DataSet paramDs)
		{			
			DataTable dt = paramDs.Tables[0];
			DataColumn dc1 = new DataColumn("StatusShow",typeof(System.String));
			DataColumn dc2 = new DataColumn("DetailLink",typeof(System.String));
			dt.Columns.Add(dc1);
			dt.Columns.Add(dc2);
			foreach(DataRow dr in dt.Rows)
			{
				if((int)dr["Status"]==(int)AppEnum.BiStatus.Valid)
					dr["StatusShow"] = "Valid";
				else if((int)dr["Status"]==(int)AppEnum.BiStatus.InValid)
					dr["StatusShow"] = "InValid";
				dr["DetailLink"] = "<a href=\"javascript:openWindowS2('SaleRuleDetail.aspx?SysNo="+dr["SysNo"].ToString()+"')\">"+dr["SysNo"].ToString()+"</a>";
			}
		}

		private void myGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			myGrid.CurrentPageIndex = e.NewPageIndex;
			BindGrid();
		}		
	}
}
